<?php
/*
 * 车主金卡类
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: de17a4664597b3c92732405c9b0e59385bb1c7f8 $
 */
class OwnCardModule extends BasicModule{

	function __construct() {
		global $table;
		parent::__construct();
		$this->_table     = $table;
		$this->_table_buy     = $table.'_buy';
	}

	/**
     * @param $post
     * @return bool 检测认证信息是否通过测试
	 */
	function pass($post) {
		if(!is_array($post)) return false;
		if(!intval($post['checknum'])) return $this->err('很抱歉,您今天的认证机会已耗尽，明天再来吧！');
		if(!$post['number']) return $this->err('请输入编号');
		if(!$post['code']) return $this->err('请输入密码');
		return true;
	}

	/**
	 * @param $post
	 * @return bool 检测订购信息是否通过测试
	 */
	function passBuy($post) {
		if(!is_array($post)) return false;
		if(!$post['truename']) return $this->err('请填写收货人姓名');
		if(!$post['buynum'] || !is_numeric($post['buynum'])) return $this->err('请填写购买数量');
		if(!$post['areaid']) return $this->err('请选择所在地区');
		if(!$post['address']) return $this->err('请填写详细地址');
		if(!$post['mobile']) return $this->err('请填写手机号码');
		if(!$post['email']) return $this->err('请填写电子邮件');
		return true;
	}

	/**
	 * @param $itemid
	 * @param $username
	 * @param $status
	 * @return bool 订购状态检测
	 */
	function checkStatus($itemid,$username,$status) {
		if(!$itemid || !$username) return false;
		$owncardAction = Db('owncard');
		$buyinfo = $owncardAction->getByitem($itemid);
		if(!$buyinfo) return false;
		if($buyinfo['username'] != $username) return false;
		if($buyinfo['status'] == 1 && $buyinfo['status'] != $status){
			dheader("?action=pay&itemid=$itemid");
			return true;
		}
		if($buyinfo['status'] == 2 && $buyinfo['status'] != $status){
			dheader("?action=success&itemid=$itemid");
			return true;
		}
		if(!$buyinfo['status']) return false;
		if( $buyinfo['status'] != $status)return false;
		return true;
	}

	/**
	 * @param $checknum
	 * @param $username
	 * @return bool 验证次数-1
	 */
	function subtract($checknum,$username) {
		global $DT_TIME;
		$users  = array();
		$checkcardDb = Db('owncard');
		$users['username'] = $username;
		$users['checktime'] = $DT_TIME;
		$users['checknum'] = $checknum-1;
		$checkcardDb->updateUsername($users);
		$userinfo = $checkcardDb->getByUsername($username);

		return $userinfo['checknum'];

	}

	/**
	 * 车主金卡是否失效
	 * @param $username
	 * @return bool
	 */
	public static function checkExpire($username) {
		global $DT_TIME;
		$checkcardDb = Db('owncard');
		$users =  $checkcardDb->getByUsername($username);

		if($users['status']){
			if($users['expiretime'] <= $DT_TIME) return false;
		}else{
			return false;
		}
		return true;
	}

	/**
	 * @param string $condition     条件
	 * @param string $order         排序方式
	 * @param string $cache         缓存
	 * @return array 获取车主列表
	 */
	function getList($condition = 'status=1', $order = 'addtime DESC', $cache = '') {
		global  $items;
		$num = $this->_db->count($this->_table, $condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = array();
		$result = $this->_db->query($sql, $cache);
		while($r = $this->_db->fetchArray($result)) {
			$lists[] = $r;
		}
		return $lists;
	}

	/**
	 * @param string $condition     条件
	 * @param string $order         排序方式
	 * @param string $cache         缓存
	 * @return array 获取订购信息列表
	 */
	function getbuyList($condition = '', $order = 'buytime DESC', $cache = '') {
		global  $items;
		$num = $this->_db->count($this->_table_buy, $condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table_buy}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = array();
		$result = $this->_db->query($sql, $cache);
		while($r = $this->_db->fetchArray($result)) {
			$lists[] = $r;
		}
		return $lists;
	}

	/**
	 * 删除条目
	 * @param      $itemid
	 * @param bool $all
	 */
	function delete($itemid, $all = true) {
		if(is_array($itemid)) {
			foreach($itemid as $v) {
				$this->delete($v, $all);
			}
		} else {
			if($all) {
				$this->_db->query(Sql::delete($this->_table_buy,"itemid=$itemid"));
			}
		}
	}

	/**
	 * 删除车主
	 * @param      $uname
	 * @param bool $all
	 */
	function delmember($uname, $all = true) {
		if(is_array($uname)) {
			foreach($uname as $v) {
				$this->delmember($v, $all);
			}
		} else {
			if($all) {

				$this->_db->query(Sql::delete($this->_table,"username='$uname'"));
			}
		}
	}
}